package com.baomidou.plugin.idea.mybatisx.advance.processor.clazz;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.plugin.idea.mybatisx.advance.util.PsiClassUtil;
import com.baomidou.plugin.idea.mybatisx.advance.util.PsiMethodUtil;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 * 给po的每个属性都生成一个one方法
 * @author wanglei
 */
public class SelectProcessor extends AbstractMPOnceMethodGenClassProcessor {

    private static final String CLASS_NAME = TableName.class.getName();

    public static final String METHOD_NAME = "select";

    public SelectProcessor() {
        super(CLASS_NAME, PsiMethod.class);
    }

    @Override
    String getMethodName() {
        return METHOD_NAME;
    }

    @NotNull
    public Collection<PsiMethod> createToStringMethod(@NotNull PsiClass psiClass, @NotNull PsiElement psiNavTargetElement) {
        final PsiMethod[] classMethods = PsiClassUtil.collectClassMethodsIntern(psiClass);
        if (PsiMethodUtil.hasMethodByName(classMethods, METHOD_NAME)) {
            return new ArrayList<>(0);
        }
        final StringBuilder builder = new StringBuilder();
        try {
            builder.append("public " + psiClass.getName() + " select(String... fields){return new " + psiClass.getName() + "();} ");
            return Collections.singletonList(PsiMethodUtil.createMethod(psiClass, builder.toString(), psiNavTargetElement));
        } finally {
            //StringBuilderSpinAllocator.dispose(builder);
        }
    }

}
